/**
 * 
 */
package com.door.erp.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.door.erp.dwz.DwzResponse;
import com.door.erp.enums.BaseInfoStatus;
import com.door.erp.model.Product;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.ProductService;

/**
 * @author xiaolongwei@gmail.com
 *
 */
@Controller
@RequestMapping(value = "/product")
public class ProductController {
	@Resource(name="productService", type=ProductService.class)
	private ProductService productService;
	@RequestMapping(value = "/save.do",method=RequestMethod.POST)
	public ModelAndView  save(Product product,@RequestParam("file")   CommonsMultipartFile  file,HttpServletRequest request) {
		//productService.add(product);
	    
		File Imgdir = new File(request.getServletContext().getRealPath("/attachment/product"));
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("product");	
		String fileName = productService.saveProductImg(file, Imgdir);
		product.setImagePath("/attachment/product/"+fileName);
		product.setStatus(BaseInfoStatus.NORMAL);
		productService.add(product);
		ModelAndView mv = new ModelAndView("dwzresponse");
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/add.do")
	public  ModelAndView  add() {
		ModelAndView mv = new ModelAndView("product/add");
	//	mv.addObject("stock", StockManager.pop());
		return mv;
	}
	@RequestMapping(value = "/edit/{id}.do")
	public  ModelAndView  edit(@PathVariable(value="id") Long id ) {
		ModelAndView mv = new ModelAndView("product/edit");
		Product product = productService.getObject(id);
		mv.addObject("product", product);
		return mv;
	}
	@RequestMapping(value = "/update.do")
	public ModelAndView update(Product product,@RequestParam("file")   CommonsMultipartFile  file,HttpServletRequest request) {
		File Imgdir = new File(request.getServletContext().getRealPath("/attachment/product"));
		DwzResponse resonse = new DwzResponse();
		resonse.setNavTabId("product");
		Product p = productService.getObject(product.getId());	
		if(file!=null){
			String fileName = productService.saveProductImg(file, Imgdir);
			product.setImagePath("/attachment/product/"+fileName);
		}
		else{
			product.setImagePath(p.getImagePath());
		}
		product.setStatus(p.getStatus());
		productService.modify(product);
		ModelAndView mv = new ModelAndView("dwzresponse");
		mv.addObject("response", resonse);		
		return mv;
	}
	@RequestMapping(value = "/delete/{id}.do")
	public ModelAndView delete(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("dwzresponse");
		Product product = productService.getObject(id);
		product.setStatus(BaseInfoStatus.DELETED);
		productService.modify(product);
	//	mv.addObject("stock", StockManager.pop());
		DwzResponse resonse = new DwzResponse();
		resonse.setStatusCode("200");
		resonse.setMessage("添加成功");	
		mv.addObject("response", resonse);
		return mv;
	}
	@RequestMapping(value = "/find/{id}.do")
	public ModelAndView find(@PathVariable(value="id") Long id) {
		ModelAndView mv = new ModelAndView("product/detail");
		Product product = productService.getObject(id);
		mv.addObject("product", product);
		return mv;
	}
	@RequestMapping(value = "index.do")
	public ModelAndView index(Pager pager) {
		ModelAndView mv = new ModelAndView("product/productIndex");
		Map<String,Object> params=new HashMap<String,Object>();
		List<Product> products =productService.getValidateResultSet(params, pager);
		mv.addObject("products", products);
		mv.addObject("pager", pager);
		return mv;
	}
	@RequestMapping(value = "findAll.do")
	public ModelAndView findAll(Pager pager) {
		ModelAndView mv = new ModelAndView("product/productIndex");
		Map<String,Object> params=new HashMap<String,Object>();
		List<Product> products =productService.getValidateResultSet(params, pager);
		mv.addObject("products", products);
		mv.addObject("pager", pager);
		return mv;
	}
	
}
